package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjStore;
import com.zb.entity.ZjStoreOutboundMaterial;
import com.zb.entity.ZjStorePosition;
import com.zb.extend.StoreOutboundMaterialExtend;
import com.zb.mapper.ZjStoreOutboundMaterialMapper;
import com.zb.service.ZjStoreOutboundMaterialService;
import com.zb.service.ZjStorePositionService;
import com.zb.service.ZjStoreService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 出库物料表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-08
 */
@Service
public class ZjStoreOutboundMaterialServiceImpl extends ServiceImpl<ZjStoreOutboundMaterialMapper, ZjStoreOutboundMaterial> implements ZjStoreOutboundMaterialService {
    @Resource
    private ZjStoreOutboundMaterialMapper storeOutboundMaterialMapper;
    @Resource
    private ZjStoreService storeService;
    @Resource
    private ZjStorePositionService storePositionService;

    // 根据出库ID获取出库物料仓库
    @Override
    public List<StoreOutboundMaterialExtend> getOutboundMaterialStoreByOutboundIds(List<Long> outboundIds) {
        if (CollectionUtil.isEmpty(outboundIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreOutboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_outbound_id", "store_id", "store_position_id");
        wrapper.in("store_outbound_id", outboundIds);
        List<ZjStoreOutboundMaterial> materialList = this.list(wrapper);

        List<ZjStore> storeList = storeService.getStoreNameByIds(materialList.stream().map(ZjStoreOutboundMaterial::getStoreId).distinct().toList());
        List<ZjStorePosition> positionList = storePositionService.getStorePositionNameByIds(materialList.stream().filter(f -> null != f.getStorePositionId() && f.getStorePositionId() > 0)
                .map(ZjStoreOutboundMaterial::getStoreId).distinct().toList());

        return materialList.stream().map(mater -> {
            StoreOutboundMaterialExtend extend = new StoreOutboundMaterialExtend();
            BeanUtil.copyProperties(mater, extend);
            storeList.stream().filter(f -> Objects.equals(f.getId(), mater.getStoreId())).map(ZjStore::getName).findFirst().ifPresent(extend::setStore);
            positionList.stream().filter(f -> Objects.equals(f.getId(), mater.getStorePositionId())).map(ZjStorePosition::getName).findFirst().ifPresent(extend::setStorePositioin);
            return extend;
        }).toList();
    }

    // 根据出库ID获取出库物料详情&即时库存
    @Override
    public List<StoreOutboundMaterialExtend> getOutboundMaterial(Long outboundId) {
        return storeOutboundMaterialMapper.getOutboundMaterial(outboundId);
    }

    // 根据出库单ID获取出库物料
    @Override
    public List<ZjStoreOutboundMaterial> getByOutboundId(Long outboundId) {
        QueryWrapper<ZjStoreOutboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_outbound_id", "type", "store_id", "store_position_id", "goods_id", "goods_spec_id", "price", "qty");
        wrapper.eq("store_outbound_id", outboundId);
        return this.list(wrapper);
    }

    // 根据仓位ID分组统计预出库数量
    @Override
    public List<ZjStoreOutboundMaterial> getOutStoreQtyGroupByStorePositionIds(List<Long> storePositionIds) {
        if (CollectionUtil.isEmpty(storePositionIds)) {
            return new ArrayList<>();
        }
        return storeOutboundMaterialMapper.getOutStoreQtyGroupByStorePositionIds(storePositionIds);
    }

    // 根据商品ID分组统计预出库数量
    @Override
    public List<ZjStoreOutboundMaterial> getOutStoreQtyByStoreIdOrStorePositionIdAndGroupByGoodsId(Long storeId, Long storePositionId, List<Long> goodsIds, List<Long> goodsSpecIds) {
        if (CollectionUtil.isEmpty(goodsIds) || CollectionUtil.isEmpty(goodsSpecIds)) {
            return new ArrayList<>();
        }
        return storeOutboundMaterialMapper.getOutStoreQtyByStoreIdOrStorePositionIdAndGroupByGoodsId(storeId, storePositionId, goodsIds, goodsSpecIds);
    }

    // 根据仓库ID分组统计预出库数量
    @Override
    public List<ZjStoreOutboundMaterial> getOutStoreQtyByStoreIdGroupByStoreId(List<Long> storeIds, List<Long> goodsIds, List<Long> goodsSpecIds) {
        if (CollectionUtil.isEmpty(storeIds) || CollectionUtil.isEmpty(goodsIds) || CollectionUtil.isEmpty(goodsSpecIds)) {
            return new ArrayList<>();
        }
        return storeOutboundMaterialMapper.getOutStoreQtyByStoreIdGroupByStoreId(storeIds, goodsIds, goodsSpecIds);
    }

    // 根据出库ID更新出库的状态
    @Override
    public void updateStatusByOutbountId(Long outboundId, Integer status) {
        storeOutboundMaterialMapper.updateStatusByOutbountId(outboundId, status);
    }

    // 根据删除的仓位ID获取关联出库物料的仓位ID
    @Override
    public List<Long> getStorePositionIdsByDelStorePositionIds(List<Long> delStorePositionIds) {
        if (CollectionUtil.isEmpty(delStorePositionIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreOutboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.last("limit 10")
                .select("store_position_id")
                .in("store_position_id", delStorePositionIds);
        return this.list(wrapper).stream().map(ZjStoreOutboundMaterial::getStorePositionId).distinct().toList();
    }

    // 根据仓库ID获取关联出库物料的仓库ID
    @Override
    public List<Long> getByStoreIds(List<Long> storeIds) {
        if (CollectionUtil.isEmpty(storeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreOutboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("store_id")
                .in("store_id", storeIds)
                .last("limit 10");
        return this.list(wrapper).stream().map(ZjStoreOutboundMaterial::getStoreId).distinct().toList();
    }
}
